//
//  TypeViewController.m
//  pocketmedicine
//
//  Created by student on 16/4/22.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "TypeViewController.h"
#import "AnalysisHelper.h"
#import "DrugClassController.h"
#import "MedicineModel.h"
#import <SVProgressHUD.h>
@interface TypeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UITableView *tableViewList;
@property (strong,nonatomic)MedicineModel *medicine;
@property (strong,nonatomic) NSArray *arr;
@property (strong,nonatomic) NSMutableArray *listarr;
@property (strong,nonatomic) NSMutableArray *drugIdList;

@end

@implementation TypeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableViewList.delegate = self;
    self.tableViewList.dataSource = self;
    
    
    NSString *httpUrl = @"http://www.tngou.net/api/drug/classify";

    [self request:httpUrl];
}



- (void)request:(NSString *)httpUrl
{    [SVProgressHUD showWithStatus:@"正在加载"];
    // 网址字符串
    NSString *urlStr = [[NSString alloc]initWithFormat: @"%@", httpUrl];
    // 网址
    // 网址
    NSURL *url = [NSURL URLWithString: urlStr];
    // 请求
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval: 10];
    // GET请求
    [request setHTTPMethod: @"GET"];
    // 设置请求头
    [request addValue: @"639a5ad1fd83cbabd5b467f9201a5c8c" forHTTPHeaderField: @"apikey"];
    
    [NSURLConnection sendAsynchronousRequest: request
                                       queue: [NSOperationQueue mainQueue]
                           completionHandler: ^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error) {
                                   NSLog(@"Httperror: %@%ld", error.localizedDescription, error.code);
                               } else {
                                   NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
                                   NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   //                                   NSLog(@"HttpResponseCode:%ld", responseCode);
                                   //                                   NSLog(@"HttpResponseBody %@",responseString);
                                   
                                   NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
                                   self.arr = [dict objectForKey:@"tngou"];
                                   //                                   NSLog(@"%@",self.arr[0]);
                                   //初始化成员变量
                                   _listarr = [[NSMutableArray alloc]init];
                                   self.drugIdList = [[NSMutableArray alloc] init];
                                   
                                   for (int i = 0; i<self.arr.count; i++) {
                                       
                                       //按数组中的索引取出对应的字典
                                       NSDictionary *listdic = [self.arr objectAtIndex:i];
                                       
                                       
                                       NSString *drugId = (NSString *)[listdic objectForKey:@"id"];
                                       //通过字典中的key取出对应value，并且强制转化为NSString类型
                                       NSString *teamname = (NSString *)[listdic objectForKey:@"title"];
                                       
                                       //将获取的value值放到数组容器中
                                       [_listarr addObject:teamname];
                                       [self.drugIdList addObject:drugId];
                                       NSLog(@"title内容为--》%@", teamname );
                                   }
                                   
                                   [self.tableViewList reloadData];
                                   
                               }
                               
                               
                               if (self.tableViewList!=nil) {
                                   self.tableViewList.hidden = NO;
                                   [SVProgressHUD dismiss];
                                   
                               }else {
                                   self.tableViewList.hidden = YES;
                                   [SVProgressHUD showWithStatus:@"加载错误"];
                               }

                           }];
    
    
}



                               
#pragma mark - Table view data source


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _listarr.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    DrugClassController *dc = [self.storyboard instantiateViewControllerWithIdentifier:@"DcVC"];
     dc.drugId = self.drugIdList[indexPath.row];
     [self presentViewController:dc animated:YES completion:nil];
   
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    
    cell.textLabel.text = [_listarr objectAtIndex:indexPath.row];
    
    return cell;
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
